package com.park.controller;

import com.park.common.annotation.NotProtected;
import com.park.common.bean.MenuInputView;
import com.park.common.bean.ResponseBean;
import com.park.common.constant.IDBConstant;
import com.park.common.constant.IPlatformConstant;
import com.park.common.exception.MessageException;
import com.park.common.poo.BannerInfo;
import com.park.common.poo.Config;
import com.park.common.poo.PersonalityInfo;
import com.park.common.poo.UserOperator;
import com.park.common.util.StrUtil;
import com.park.platform.service.IBannerService;
import com.park.platform.service.IConfigService;
import com.park.platform.service.IMenuService;
import com.park.platform.service.IOperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("passport")
public class PassportController extends BaseController {

    @Autowired
    private IOperatorService operatorService;
    
    @Autowired
    private IMenuService menuService;

    @Autowired
    private IBannerService bannerService;

    @Autowired
    private IConfigService configService;

    // 登录
    @NotProtected
    @RequestMapping("login")
    public String passportLogin(String returnUrl, Model model) {
        return "Passport/PassportLogin";
    }

    @NotProtected
    @ResponseBody
    @RequestMapping("submitUserLogin")
    public ResponseBean submitUserLogin(String name, String pwd, HttpServletResponse response, HttpSession session) {
        try{
            if(StrUtil.isBlank(name)) throw new MessageException("请输入用户名！");
            if(StrUtil.isBlank(pwd)) throw new MessageException("请输入密码！");
            UserOperator operator = operatorService.loginIn(name, pwd);
            if(operator == null) throw new MessageException("用户名错误！");
            if(!pwd.equals(operator.getOperatorPwd())) throw new MessageException("密码错误！");
            if(!IDBConstant.LOGIC_STATUS_YES.equals(operator.getStatus())) throw new MessageException("您的帐号已被锁定，请联系管理员！");
            operator.setOperatorPwd(null);
            super.getRequest().getSession().setAttribute(IPlatformConstant.LOGIN_USER_IN, operator);
            //获取菜单
            MenuInputView menuInputView = new MenuInputView();
			menuInputView.setId(super.getUserInfo2().getId());
            session.setAttribute("menus", menuService.getMenus(menuInputView));
            super.setUserOperatorCookie(name, pwd, response);
            return new ResponseBean(true);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    //退出登录
    @RequestMapping("logout")
    public String logout(HttpServletResponse response) {
        System.out.println("=====out=====");
        super.getRequest().getSession().invalidate();
        super.clearUserOperatorCookie(response);
        return redirect("/passport/login");
    }

    // 修改密码
    @RequestMapping("modifyPassword")
    public String modifyPassword() {
        return "Center/CenterPasswordModification";
    }

    @ResponseBody
    @RequestMapping("updatePwd")
    public ResponseBean updatePwd(String oldPwd, String newPwd, String confirmPwd){
        try {
            if(StrUtil.isBlank(oldPwd)) throw new MessageException("请输入原密码");
            if(StrUtil.isBlank(newPwd)) throw new MessageException("请输入新密码");
            if(StrUtil.isBlank(confirmPwd)) throw new MessageException("请输入确认密码");
            if(!newPwd.equals(confirmPwd)) throw new MessageException("两次密码不一致");
           operatorService.updatePassword(oldPwd, newPwd, super.getUserInfo2().getId());
            return new ResponseBean(true);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @RequestMapping(value = "getBanners")
    public String getBanners(Model model) {
        try {
            model.addAllAttributes(bannerService.getBanners());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Banner/Banners";
    }

    @ResponseBody
    @RequestMapping("saveBanner")
    public ResponseBean saveBanner(BannerInfo bannerInfo, MultipartHttpServletRequest multipartRequest){
        try {
            Integer bannerId = bannerService.saveBanner(bannerInfo, multipartRequest);
            Map<String, Object> data = new HashMap<>();
            data.put("bannerId", bannerId);
            return new ResponseBean(data);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("newDeleteBanner")
    public ResponseBean newDeleteBanner(int bannerId){
        try {
            bannerService.newDeleteBanner(bannerId);
            return new ResponseBean(true);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @RequestMapping(value = "getConfig")
    public String getConfig(Model model) {
        try {
            model.addAttribute("config", configService.getConfig());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Banner/commission";
    }

    @ResponseBody
    @RequestMapping("saveConfig")
    public ResponseBean saveConfig(Config config){
        try {
            configService.saveConfig(config);
            return new ResponseBean(true);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @RequestMapping(value = "getPersonalitys")
    public String getPersonalitys(Model model) {
        try {
            model.addAllAttributes(bannerService.getPersonalitys());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Personality/Personalitys";
    }

    @ResponseBody
    @RequestMapping("savePersonality")
    public ResponseBean savePersonality(PersonalityInfo personalityInfo, MultipartHttpServletRequest multipartRequest){
        try {
            Integer bannerId = bannerService.savePersonality(personalityInfo, multipartRequest);
            Map<String, Object> data = new HashMap<>();
            data.put("bannerId", bannerId);
            return new ResponseBean(data);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("newDeletePersonality")
    public ResponseBean newDeletePersonality(int personalityId){
        try {
            bannerService.newDeletePersonality(personalityId);
            return new ResponseBean(true);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

}
